package cn.yanhu.kuwanapp.my.fragment

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import cn.woochen.comlib.BaseFragment
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.bean.response.RespWithdrawRecordBean
import cn.yanhu.kuwanapp.databinding.FragmentWidrawRecordBinding
import cn.yanhu.kuwanapp.my.adapter.WithdrawRecordAdapter
import cn.yanhu.kuwanapp.user.UserViewModel
import cn.yanhu.kuwanapp.utils.ext.parseState
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 提现记录(all，income，withdraw)
 */
class WithdrawRecordFragment : BaseFragment<FragmentWidrawRecordBinding, UserViewModel>() {

    private val mWithdrawRecordDatas by lazy {
        mutableListOf<RespWithdrawRecordBean.Record>()
    }

    private val mWithdrawRecordAdapter by lazy {
        WithdrawRecordAdapter(mWithdrawRecordDatas)
    }
    private val mExtraIndex by lazy {
        this.arguments?.getInt(EXTRA_INDEX, 0)?:0
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.withdrawRecordsInfo.observe(this, Observer { it ->
            parseState(it, {
                val list = it.info
                if (page == 1){
                    mWithdrawRecordDatas.clear()
                }else{
                    if (list.isEmpty()) mBinding.refreshLayout.finishLoadMoreWithNoMoreData() else mBinding.refreshLayout.finishLoadMore()
                }
                mWithdrawRecordDatas.addAll(list)
                if (mWithdrawRecordDatas.isEmpty()) showEmpty()
                mWithdrawRecordAdapter.notifyDataSetChanged()
            })
        })

    }

    override fun setContentView(): Int = R.layout.fragment_widraw_record

    override fun initData() {
        initWidgets()
    }

    override fun onResume() {
        super.onResume()
        requestData()
    }

    private var page = 1
    private val pageSize = 20

    override fun requestData() {
        mViewModel.withdrawRecords(mExtraIndex + 1,page,pageSize)
    }

    private fun initWidgets() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener( object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                requestData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                requestData()
                refreshLayout.finishRefresh()
            }
        })
        mBinding.rvList.adapter = mWithdrawRecordAdapter
    }

    override fun showEmpty() {
        mBinding.refreshLayout.isVisible = false
        mBinding.llEmpty.isVisible = true
    }


    override fun showLoading(showContent: Boolean) {
        super.showLoading(showContent)
        mBinding.refreshLayout.isVisible = true
        mBinding.llEmpty.isVisible = false
    }

    companion object {
        private const val EXTRA_INDEX = "extra_index"
        fun newInstance(index: Int): WithdrawRecordFragment = WithdrawRecordFragment().apply {
            arguments = Bundle().apply {
                putInt(EXTRA_INDEX, index)
            }
        }
    }
}